{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-03-04T06:29:42.852160Z",
     "start_time": "2024-03-04T06:29:42.843178Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number is 1\n",
      "number is 2\n",
      "number is 3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# while循环\n",
    "number  = 1\n",
    "while number <= 3:\n",
    "    print(f\"number is {number}\")\n",
    "    # number_temp = number + 1\n",
    "    # number = number_temp\n",
    "    number += 1\n",
    "\n",
    "print(number)"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number is 3\n",
      "number is 2\n",
      "number is 1\n"
     ]
    }
   ],
   "source": [
    "number = 3\n",
    "while number >= 1:\n",
    "    print(f\"number is {number}\")\n",
    "    number -= 1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T06:30:33.818549Z",
     "start_time": "2024-03-04T06:30:33.815059Z"
    }
   },
   "id": "bde334475c83dcd9",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'list1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[7], line 3\u001B[0m\n\u001B[1;32m      1\u001B[0m number  \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m1\u001B[39m\n\u001B[1;32m      2\u001B[0m \u001B[38;5;28;01mwhile\u001B[39;00m number \u001B[38;5;241m<\u001B[39m\u001B[38;5;241m=\u001B[39m \u001B[38;5;241m3\u001B[39m:\n\u001B[0;32m----> 3\u001B[0m     \u001B[43mlist1\u001B[49m[\u001B[38;5;241m1\u001B[39m]\n\u001B[1;32m      4\u001B[0m     \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mnumber is \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mnumber\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m      5\u001B[0m     \u001B[38;5;66;03m# number_temp = number + 1\u001B[39;00m\n\u001B[1;32m      6\u001B[0m     \u001B[38;5;66;03m# number = number_temp\u001B[39;00m\n",
      "\u001B[0;31mNameError\u001B[0m: name 'list1' is not defined"
     ]
    }
   ],
   "source": [
    "#循环结束，正常退出\n",
    "#循环执行出错，异常退出\n",
    "number  = 1\n",
    "while number <= 3:\n",
    "    # list1[1]\n",
    "    print(f\"number is {number}\")\n",
    "    number += 1\n",
    "\n",
    "print(number)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T06:31:35.731818Z",
     "start_time": "2024-03-04T06:31:35.615371Z"
    }
   },
   "id": "885530379fb2cf3b",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number is True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#使用标志退出\n",
    "number  = True\n",
    "while number:\n",
    "    print(f\"number is {number}\")\n",
    "    number = False\n",
    "\n",
    "print(number)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T06:35:27.945992Z",
     "start_time": "2024-03-04T06:35:27.943698Z"
    }
   },
   "id": "ce07a30d6310b63a",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number is 9\n",
      "number is 8\n",
      "number is 7\n",
      "number is 6\n",
      "number is 4\n",
      "number is 3\n",
      "number is 2\n",
      "number is 1\n",
      "number is 0\n"
     ]
    }
   ],
   "source": [
    "#break、continue退出循环程序\n",
    "number = 10\n",
    "while number >= 1:\n",
    "    number -= 1\n",
    "    if number == 5:\n",
    "        # break\n",
    "        continue\n",
    "    print(f\"number is {number}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T06:40:25.237570Z",
     "start_time": "2024-03-04T06:40:25.233009Z"
    }
   },
   "id": "a59bc3de6cd56f7a",
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "#条件不满足，不执行循环\n",
    "#死循环，不退出\n",
    "# while True："
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T06:42:55.948536Z",
     "start_time": "2024-03-04T06:42:55.945587Z"
    }
   },
   "id": "e875e17a218fa3ed",
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "#循环结果放入列表\n",
    "list1 = []\n",
    "number = 1\n",
    "while number < 5:\n",
    "    list1.append(number)\n",
    "    number += 1\n",
    "\n",
    "print(list1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T06:44:19.555045Z",
     "start_time": "2024-03-04T06:44:19.551596Z"
    }
   },
   "id": "5a336fdf73f08c07",
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "4\n",
      "3\n",
      "2\n",
      "1\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "#循环操作列表\n",
    "list1 = [1,2,3,4,5]\n",
    "while list1:\n",
    "    print(list1.pop())\n",
    "\n",
    "print(list1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-04T06:45:17.639894Z",
     "start_time": "2024-03-04T06:45:17.634710Z"
    }
   },
   "id": "9fa522cae68c8ea9",
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "#练习\n",
    "#编写一个猜数字的游戏程序。随机产生一个 100 以内的整数，并要求用户输入三次数据，当任意一次输入的数字和随机数相同，游戏结束。否则需程序运行三次后，提示正确答案，退出程序。\n",
    "#提示： 使用 random.randint(1, 10) 函数，可以产生指定范围的随机数"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T06:30:27.161542Z",
     "start_time": "2024-03-05T06:30:27.157466Z"
    }
   },
   "id": "8e9f521fbf35565c",
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你还有0次机会竞猜\n",
      "你还有1次机会竞猜\n",
      "你还有2次机会竞猜\n",
      "正确的数字是：28\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "a = 0\n",
    "num = random.randint(1, 100)\n",
    "while a < 3:\n",
    "    my_num = input(\"请输入整数\")\n",
    "    if my_num == num:\n",
    "        print(\"你猜对了\")\n",
    "        break        \n",
    "    print(f\"你还有{a}次机会竞猜\")\n",
    "    a += 1\n",
    "\n",
    "print(f\"正确的数字是：{num}\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-05T06:39:42.943723Z",
     "start_time": "2024-03-05T06:39:38.468708Z"
    }
   },
   "id": "7534170e4bf8625",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "d374b048cf51567f"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
